/*******************************************************************************
* File Name: CSD_Touchpad_CSHL.c
* Version 2.40
*
* Description:
*  This file provides the source code to the High Level APIs for the CapSesne
*  CSD component.
*
* Note:
*
********************************************************************************
* Copyright 2013-2015, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#include "CSD_Touchpad_CSHL.h"
#include "CSD_Touchpad_PVT.h"

/* SmartSense functions */
#if (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)
    extern void CSD_Touchpad_UpdateThresholds(uint32 sensor);
#endif /* (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO) */

/* Storage of filters data */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER)) )

    uint16 CSD_Touchpad_rawFilterData1[CSD_Touchpad_TOTAL_SENSOR_COUNT];
    uint16 CSD_Touchpad_rawFilterData2[CSD_Touchpad_TOTAL_SENSOR_COUNT];

#elif ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER))   || \
        (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER))   || \
        (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)) || \
        (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER))   || \
        (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER)) )

    uint16 CSD_Touchpad_rawFilterData1[CSD_Touchpad_TOTAL_SENSOR_COUNT];
#endif  /* ( (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER) || \
        *    (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER) )
        */

extern uint16 CSD_Touchpad_sensorRaw[CSD_Touchpad_TOTAL_SENSOR_COUNT];
extern uint8 CSD_Touchpad_sensorEnableMask[CSD_Touchpad_TOTAL_SENSOR_MASK];
extern const uint8 CSD_Touchpad_widgetNumber[CSD_Touchpad_TOTAL_SENSOR_COUNT];

#if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    extern uint32 CSD_Touchpad_widgetResolution[CSD_Touchpad_RESOLUTIONS_TBL_SIZE];
#endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */

uint16 CSD_Touchpad_sensorBaseline[CSD_Touchpad_TOTAL_SENSOR_COUNT] = {0u};
uint8 CSD_Touchpad_sensorBaselineLow[CSD_Touchpad_TOTAL_SENSOR_COUNT] = {0u};
uint8 CSD_Touchpad_sensorSignal[CSD_Touchpad_TOTAL_SENSOR_COUNT] = {0u};
uint8 CSD_Touchpad_sensorOnMask[CSD_Touchpad_TOTAL_SENSOR_MASK] = {0u};

uint8 CSD_Touchpad_lowBaselineResetCnt[CSD_Touchpad_TOTAL_SENSOR_COUNT];
uint8 CSD_Touchpad_lowBaselineReset[CSD_Touchpad_TOTAL_SENSOR_COUNT] = {
    5u, 5u, 5u, 5u, 5u, 5u, 5u, 5u, 5u, 5u, 
};



#if (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)
    extern CSD_Touchpad_CONFIG_TYPE_P4_v2_40 CSD_Touchpad_config;
#endif  /* (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO) */

#if (CSD_Touchpad_RAW_FILTER_MASK != 0u)
uint8 CSD_Touchpad_enableRawFilters = CSD_Touchpad_RAW_FILTERS_ENABLED;
#endif  /* (CSD_Touchpad_RAW_FILTER_MASK != 0u) */

/* Generated by Customizer */
uint8 CSD_Touchpad_fingerThreshold[] = {
    12u, 12u, 
};

uint8 CSD_Touchpad_noiseThreshold[] = {
    17u, 17u, 
};

uint8 CSD_Touchpad_negativeNoiseThreshold[] = {
    20u, 20u, 
};

uint8 CSD_Touchpad_hysteresis[] = {
    0u, 0u, 
};

uint8 CSD_Touchpad_debounce[] = {
    1u, 1u, 
};

const uint8 CSD_Touchpad_rawDataIndex[] = {
    0u, /* Trackpad__TPCol */
    5u, /* Trackpad__TPRow */
};

const uint8 CSD_Touchpad_numberOfSensors[] = {
    5u, /* Trackpad__TPCol */
    5u, /* Trackpad__TPRow */
};

uint32 CSD_Touchpad_centroidMult[] = {
    20480u, 20480u, 
};

uint8 CSD_Touchpad_posFiltersMask[] = {
    0x19u, 0x0u, 
};




/*******************************************************************************
* Function Name: CSD_Touchpad_BaseInit
********************************************************************************
*
* Summary:
*  Loads the CSD_Touchpad_sensorBaseline[sensor] array element with an
*  initial value which is equal to the raw count value.
*  Resets to zero CSD_Touchpad_sensorBaselineLow[sensor] and
*  CSD_Touchpad_sensorSignal[sensor] array element.
*  Loads the CSD_Touchpad_rawFilterData2[sensor] and
*  CSD_Touchpad_rawFilterData2[sensor] array element with an
*  initial value which is equal to the raw count value if the raw data filter is enabled.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorBaseline[]    - used to store the baseline value.
*  CSD_Touchpad_sensorBaselineLow[] - used to store the fraction byte of
*  the baseline value.
*  CSD_Touchpad_sensorSignal[]      - used to store a difference between
*  the current value of raw data and the previous value of the baseline.
*  CSD_Touchpad_rawFilterData1[]    - used to store a previous sample of
*  any enabled raw data filter.
*  CSD_Touchpad_rawFilterData2[]    - used to store before a previous sample
*  of the enabled raw data filter. Required only for median or average filters.
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_BaseInit(uint32 sensor)
{
    #if ((CSD_Touchpad_TOTAL_BUTTONS_COUNT) || (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT) || \
         (CSD_Touchpad_TOTAL_GENERICS_COUNT))
        uint8 widget = CSD_Touchpad_widgetNumber[sensor];
    #endif /* ((CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT) || (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT)) */

    #if (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT)
        uint8 debounceIndex;
    #endif  /* (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT) */

    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        /* Exclude generic widget */
        if(widget < CSD_Touchpad_END_OF_WIDGETS_INDEX)
        {
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */

    /* Initialize Baseline */
    CSD_Touchpad_sensorBaseline[sensor] = CSD_Touchpad_sensorRaw[sensor];
    CSD_Touchpad_sensorBaselineLow[sensor] = 0u;
    CSD_Touchpad_sensorSignal[sensor] = 0u;



    #if ((0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) ||\
         (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER)))

        CSD_Touchpad_rawFilterData1[sensor] = CSD_Touchpad_sensorRaw[sensor];
        CSD_Touchpad_rawFilterData2[sensor] = CSD_Touchpad_sensorRaw[sensor];

    #elif ((CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER) ||\
           (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER) ||\
           (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER) ||\
           (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER) ||\
           (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER))

        CSD_Touchpad_rawFilterData1[sensor] = CSD_Touchpad_sensorRaw[sensor];

    #else
        /* No Raw filters */
    #endif  /* ((CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER) || \
            *   (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER))
            */

    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        /* Exclude generic widget */
        }
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */
}


/*******************************************************************************
* Function Name: CSD_Touchpad_InitializeSensorBaseline
********************************************************************************
*
* Summary:
*  Loads the CSD_Touchpad_sensorBaseline[sensor] array element with an
*  initial value by scanning the selected sensor (one channel design) or a pair
*  of sensors (two channels designs). The raw count value is copied into the
*  baseline array for each sensor. The raw data filters are initialized if
*  enabled.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  None
*
* Global Variables:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_InitializeSensorBaseline(uint32 sensor)
{
    /* Scan sensor */
    CSD_Touchpad_ScanSensor(sensor);
    while(CSD_Touchpad_IsBusy() != 0u)
    {
        /* Wait while sensor is busy */
    }

    /* Initialize Baseline, Signal and debounce counters */
    CSD_Touchpad_BaseInit(sensor);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_InitializeAllBaselines
********************************************************************************
*
* Summary:
*  Uses the CSD_Touchpad_InitializeSensorBaseline function to load the
*  CSD_Touchpad_sensorBaseline[] array with an initial values by scanning
*  all the sensors. The raw count values are copied into the baseline array for
*  all the sensors. The raw data filters are initialized if enabled.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Global Variables:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_InitializeAllBaselines(void)
{
    uint32 i;

    for(i = 0u; i < CSD_Touchpad_TOTAL_SCANSLOT_COUNT; i++)
    {
        CSD_Touchpad_InitializeSensorBaseline(i);
    }
}


/*******************************************************************************
* Function Name: CSD_Touchpad_InitializeEnabledBaselines
********************************************************************************
*
* Summary:
*  Scans all the enabled widgets and the raw count values are copied into the
*  baseline array for all the sensors enabled in the scanning process. The baselines
*  are initialized with zero values for sensors disabled from the scanning process.
*  The raw data filters are initialized if enabled.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorRaw[] - used to store the sensors raw data.
*  CSD_Touchpad_sensorEnableMask[ ] - used to store bit masks of the enabled sensors.
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_InitializeEnabledBaselines(void)
{
    uint32 i;
    uint32 isSensorEnabled;

    CSD_Touchpad_ScanEnabledWidgets();
    while(CSD_Touchpad_IsBusy() != 0u)
    {
        /* Wait while sensor is busy */
    }

    for(i = 0u; i < CSD_Touchpad_TOTAL_SENSOR_COUNT; i++)
    {
        isSensorEnabled = CSD_Touchpad_GetBitValue(CSD_Touchpad_sensorEnableMask, i);

        /* Clear raw data if sensor is disabled from scanning process */
        if(isSensorEnabled != 0u)
        {
            /* Initialize baselines */
            CSD_Touchpad_BaseInit(i);
        }
    }
}


/*******************************************************************************
* Function Name: CSD_Touchpad_UpdateBaselineNoThreshold
********************************************************************************
*
* Summary:
*  Updates the CSD_Touchpad_sensorBaseline[sensor] array element using the
*  LP filter with k = 256. The signal calculates the difference of count by
*  subtracting the previous baseline from the current raw count value and stores
*  it in CSD_Touchpad_sensorSignal[sensor].
*  If the auto reset option is enabled, the baseline updated regards the noise threshold.
*  If the auto reset option is disabled, the baseline stops updating; baseline is loaded
*  with a raw count value if a signal is greater than zero and if signal is less
*  than noise threshold.
*  Raw data filters are applied to the values if enabled before baseline
*  calculation.
*
*  This API does not update the thresholds in the Smartsense Mode.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_widgetNumber[]  - stores widget numbers.
*  CSD_Touchpad_sensorBaseline[]    - used to store baseline value.
*  CSD_Touchpad_sensorBaselineLow[] - used to store fraction byte of
*  baseline value.
*  CSD_Touchpad_sensorSignal[]      - used to store difference between
*  current value of raw data and previous value of baseline.
*  CSD_Touchpad_rawFilterData1[]    - used to store previous sample of
*  any enabled raw data filter.
*  CSD_Touchpad_rawFilterData2[]    - used to store before previous sample
*  of enabled raw data filter. Only required for median or average filters.
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_UpdateBaselineNoThreshold(uint32 sensor)
{
    uint32 calc;
    uint32 sign;
    uint16 tempRaw;
    uint16 filteredRawData;
    uint8 widget = CSD_Touchpad_widgetNumber[sensor];
    uint8 noiseThreshold;

    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        /* Exclude generic widget */
        if(widget < CSD_Touchpad_END_OF_WIDGETS_INDEX)
        {
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */

    noiseThreshold = CSD_Touchpad_noiseThreshold[widget];
    filteredRawData = CSD_Touchpad_sensorRaw[sensor];

    #if (CSD_Touchpad_RAW_FILTER_MASK != 0u)
    if(CSD_Touchpad_enableRawFilters != 0u)
    {
        #if (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)
            tempRaw = filteredRawData;
            filteredRawData = CSD_Touchpad_MedianFilter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor],
                                                            CSD_Touchpad_rawFilterData2[sensor]);
            CSD_Touchpad_rawFilterData2[sensor] = CSD_Touchpad_rawFilterData1[sensor];
            CSD_Touchpad_rawFilterData1[sensor] = tempRaw;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER)
            tempRaw = filteredRawData;
            filteredRawData = CSD_Touchpad_AveragingFilter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor],
                                                               CSD_Touchpad_rawFilterData2[sensor]);
            CSD_Touchpad_rawFilterData2[sensor] = CSD_Touchpad_rawFilterData1[sensor];
            CSD_Touchpad_rawFilterData1[sensor] = tempRaw;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER)
            filteredRawData = CSD_Touchpad_IIR2Filter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor]);
            CSD_Touchpad_rawFilterData1[sensor] = filteredRawData;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER)
            filteredRawData = CSD_Touchpad_IIR4Filter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor]);
            CSD_Touchpad_rawFilterData1[sensor] = filteredRawData;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)
            filteredRawData = CSD_Touchpad_JitterFilter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor]);
            CSD_Touchpad_rawFilterData1[sensor] = filteredRawData;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER)
            filteredRawData = CSD_Touchpad_IIR8Filter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor]);
            CSD_Touchpad_rawFilterData1[sensor] = filteredRawData;

        #elif (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER)
            filteredRawData = CSD_Touchpad_IIR16Filter(filteredRawData, CSD_Touchpad_rawFilterData1[sensor]);
            CSD_Touchpad_rawFilterData1[sensor] = filteredRawData;

        #else
            /* No Raw filters */
        #endif  /* (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER) */
    }
    #endif  /* (CSD_Touchpad_RAW_FILTER_MASK != 0u) */

    #if (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)
        MeasureNoiseEnvelope_P4_v2_40(&CSD_Touchpad_config, (uint8)sensor, CSD_Touchpad_sensorRaw);
    #endif /* (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO) */

    /* Baseline calculation */
    /* Calculate difference RawData[cur] - Baseline[prev] */
    if(filteredRawData >= CSD_Touchpad_sensorBaseline[sensor])
    {
        CSD_Touchpad_lowBaselineResetCnt[sensor] = 0u;
        tempRaw = filteredRawData - CSD_Touchpad_sensorBaseline[sensor];
        sign = 1u;    /* Positive difference - Calculate the Signal */
    }
    else
    {
        tempRaw = CSD_Touchpad_sensorBaseline[sensor] - filteredRawData;
        sign = 0u;    /* Negative difference - Do NOT calculate the Signal */
    }

    #if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    if((sign == 0u) && (tempRaw > (uint16) CSD_Touchpad_negativeNoiseThreshold[widget]))
    #else
    if((sign == 0u) && (tempRaw > (uint16) CSD_Touchpad_NEGATIVE_NOISE_THRESHOLD))
    #endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */
    {
        if(CSD_Touchpad_lowBaselineResetCnt[sensor] >= CSD_Touchpad_lowBaselineReset[sensor])
        {
            CSD_Touchpad_BaseInit(sensor);
            CSD_Touchpad_lowBaselineResetCnt[sensor] = 0u;
        }
        else
        {
            CSD_Touchpad_lowBaselineResetCnt[sensor]++;
        }
    }
    else
    {
        #if (CSD_Touchpad_AUTO_RESET == CSD_Touchpad_AUTO_RESET_DISABLE)
        #if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
            /* Update Baseline if lower than noiseThreshold */
            if ( (tempRaw <= noiseThreshold) ||
             ((tempRaw < (uint16) CSD_Touchpad_negativeNoiseThreshold[widget])
               && (sign == 0u)))
            {
        #else
            /* Update Baseline if lower than noiseThreshold */
            if ( (tempRaw <= noiseThreshold) ||
                 ((tempRaw < (uint16) CSD_Touchpad_NEGATIVE_NOISE_THRESHOLD)
                   && (sign == 0u)))
            {
        #endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */
        #endif /* (CSD_Touchpad_AUTO_RESET == CSD_Touchpad_AUTO_RESET_DISABLE) */
                /* Make full Baseline 23 bits */
                calc = (uint32) CSD_Touchpad_sensorBaseline[sensor] << 8u;
                calc |= (uint32) CSD_Touchpad_sensorBaselineLow[sensor];

                /* Add Raw Data to Baseline */
                calc += filteredRawData;

                /* Sub the high Baseline */
                calc -= CSD_Touchpad_sensorBaseline[sensor];

                /* Put Baseline and BaselineLow */
                CSD_Touchpad_sensorBaseline[sensor] = ((uint16) (calc >> 8u));
                CSD_Touchpad_sensorBaselineLow[sensor] = ((uint8) calc);

                CSD_Touchpad_lowBaselineResetCnt[sensor] = 0u;
        #if (CSD_Touchpad_AUTO_RESET == CSD_Touchpad_AUTO_RESET_DISABLE)
            }
        #endif /* (CSD_Touchpad_AUTO_RESET == CSD_Touchpad_AUTO_RESET_DISABLE) */
    }

    /* Calculate Signal if positive difference > noiseThreshold */
    if((tempRaw > (uint16) noiseThreshold) && (sign != 0u))
    {
        #if (CSD_Touchpad_SIGNAL_SIZE == CSD_Touchpad_SIGNAL_SIZE_UINT8)
            /* Over flow defence for uint8 */
            if (tempRaw > 0xFFu)
            {
                CSD_Touchpad_sensorSignal[sensor] = 0xFFu;
            }
            else
            {
                CSD_Touchpad_sensorSignal[sensor] = ((uint8) tempRaw);
            }
        #else
            CSD_Touchpad_sensorSignal[sensor] = ((uint16) tempRaw);
        #endif  /* (CSD_Touchpad_SIGNAL_SIZE == CSD_Touchpad_SIGNAL_SIZE_UINT8) */
    }
    else
    {
        /* Signal is zero */
        CSD_Touchpad_sensorSignal[sensor] = 0u;
    }

    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        /* Exclude generic widget */
        }
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */
}


/*******************************************************************************
* Function Name: CSD_Touchpad_UpdateSensorBaseline
********************************************************************************
*
* Summary:
*  Updates the CSD_Touchpad_sensorBaseline[sensor] array element using the
*  LP filter with k = 256. The signal calculates the difference of count by
*  subtracting the previous baseline from the current raw count value and stores
*  it in CSD_Touchpad_sensorSignal[sensor].
*  If the auto reset option is enabled, the baseline updated regards the noise threshold.
*  If the auto reset option is disabled, the baseline stops updating.
*  Baseline is loaded with raw count value if a signal is greater than zero and
*  if signal is less than noise threshold.
*  Raw data filters are applied to the values if enabled before baseline
*  calculation.
*  This API updates the thresholds in the Smartsense Mode.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_widgetNumber[]  - stores widget numbers.
*
* Side Effects:
*  None
*
*******************************************************************************/
 void CSD_Touchpad_UpdateSensorBaseline(uint32 sensor)
{
    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        uint32 widget;

        widget = CSD_Touchpad_widgetNumber[sensor];

        /* Exclude generic widget */
        if(widget < CSD_Touchpad_END_OF_WIDGETS_INDEX)
        {
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */

    #if (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)
        CSD_Touchpad_UpdateThresholds(sensor);
    #endif /* (CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO) */

    CSD_Touchpad_UpdateBaselineNoThreshold(sensor);

    #if (CSD_Touchpad_TOTAL_GENERICS_COUNT)
        /* Exclude generic widget */
        }
    #endif  /* CSD_Touchpad_TOTAL_GENERICS_COUNT */
}


/*******************************************************************************
* Function Name: CSD_Touchpad_UpdateEnabledBaselines
********************************************************************************
*
* Summary:
*  Checks CSD_Touchpad_sensorEnableMask[] array and calls the
*  CSD_Touchpad_UpdateSensorBaseline function to update the baselines
*  for the enabled sensors.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorEnableMask[] - used to store the sensor scanning
*  state.
*  CSD_Touchpad_sensorEnableMask[0] contains the masked bits for sensors
*   0 through 7 (sensor 0 is bit 0, sensor 1 is bit 1).
*  CSD_Touchpad_sensorEnableMask[1] contains the masked bits for
*  sensors 8 through 15 (if needed), and so on.
*  0 - sensor doesn't scan by CSD_Touchpad_ScanEnabledWidgets().
*  1 - sensor scans by CSD_Touchpad_ScanEnabledWidgets().
*
* Side Effects:
*  None
*
*******************************************************************************/
 void CSD_Touchpad_UpdateEnabledBaselines(void)
{
    uint32 i;
    uint32 isSensorEnabled;

    for(i = 0u; i < CSD_Touchpad_TOTAL_SENSOR_COUNT; i++)
    {
        isSensorEnabled = CSD_Touchpad_GetBitValue(CSD_Touchpad_sensorEnableMask, i);

        if(0u != isSensorEnabled)
        {
            CSD_Touchpad_UpdateSensorBaseline(i);
        }
    }
}

/*******************************************************************************
* Function Name: CSD_Touchpad_UpdateWidgetBaseline
********************************************************************************
*
* Summary:
*  Checks CSD_Touchpad_sensorEnableMask[] array and calls the
*  CSD_Touchpad_UpdateSensorBaseline function to update the baselines
*  for the enabled sensors that belong to widget.
*
* Parameters:
*  widget:  Widget number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorEnableMask[] - used to store the sensor scanning
*  state.
*  CSD_Touchpad_sensorEnableMask[0] contains the masked bits for sensors
*   0 through 7 (sensor 0 is bit 0, sensor 1 is bit 1).
*  CSD_Touchpad_sensorEnableMask[1] contains the masked bits for
*  sensors 8 through 15 (if needed), and so on.
*  0 - sensor doesn't scan by CSD_Touchpad_ScanEnabledWidgets().
*  1 - sensor scans by CSD_Touchpad_ScanEnabledWidgets().
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_UpdateWidgetBaseline(uint32 widget)
{
    uint32 sensorsPerWidget;
    uint32 firstSensor;
    uint32 lastSensor;
    uint32 i;
    uint32 isSensorEnabled;

    /* Get number of sensors in widget */
    sensorsPerWidget = CSD_Touchpad_numberOfSensors[widget];

    /* Get first sensor in widget */
    firstSensor = CSD_Touchpad_rawDataIndex[widget];

    /* Get last sensor in widget */
    lastSensor = (CSD_Touchpad_rawDataIndex[widget] + sensorsPerWidget);

    for(i = firstSensor; i < lastSensor; i++)
    {
        isSensorEnabled = CSD_Touchpad_GetBitValue(CSD_Touchpad_sensorEnableMask, i);

        if(0u != isSensorEnabled)
        {
            CSD_Touchpad_UpdateSensorBaseline(i);
        }
    }
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetBaselineData
********************************************************************************
*
* Summary:
*  This function reads the sensor baseline from the component.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  This API returns the baseline value of the sensor indicated by an argument.
*
* Global Variables:
*  CSD_Touchpad_sensorBaseline[] - the array with an initial values by scanning
*
* Side Effects:
*  None
*
*******************************************************************************/
uint16 CSD_Touchpad_GetBaselineData(uint32 sensor)
{
    return (CSD_Touchpad_sensorBaseline[sensor]);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_SetBaselineData
********************************************************************************
*
* Summary:
*  This API writes the data value passed as an argument to the sensor baseline array.
*
* Parameters:
*  sensor:  Sensor number.
*  data:    Sensor baseline.
*
* Return:
*  None.
*
* Global Variables:
*  CSD_Touchpad_sensorBaseline[] - the array with initial values by scanning
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_SetBaselineData(uint32 sensor, uint16 data)
{
    CSD_Touchpad_sensorBaseline[sensor] = data;
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetDiffCountData
********************************************************************************
*
* Summary:
*  This function returns the Sensor Signal from the component.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  This API returns the difference count value of the sensor indicated by the
*  argument.
*
* Global Variables:
*  CSD_Touchpad_sensorSignal[] - the array with difference counts for sensors
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetDiffCountData(uint32 sensor)
{
    return (CSD_Touchpad_sensorSignal[sensor]);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_SetDiffCountData
********************************************************************************
*
* Summary:
*  This API writes the data value passed as an argument to the sensor signal array.
*
* Parameters:
*  sensor:  Sensor number.
*  value:   Sensor signal.
*
* Return:
*  None.
*
* Global Variables:
*  CSD_Touchpad_sensorSignal[] - the array with difference counts for sensors
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_SetDiffCountData(uint32 sensor, uint8 value)
{
    CSD_Touchpad_sensorSignal[sensor] = value;
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetFingerThreshold
********************************************************************************
*
* Summary:
*  This function reads the finger threshold from the component.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  This API returns the finger threshold of the widget indicated by the argument.
*
* Global Variables:
*  CSD_Touchpad_fingerThreshold[] - This array contains the level of signal
*  for each widget that is determined if a finger is present on the widget.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetFingerThreshold(uint32 widget)
{
    return(CSD_Touchpad_fingerThreshold[widget]);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetNoiseThreshold
********************************************************************************
*
* Summary:
*  This function reads the noise threshold from the component.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  This API returns the noise threshold of the widget indicated by the argument.
*
* Global Variables:
*  CSD_Touchpad_noiseThreshold[] - This array contains the level of signal
*  for each widget that determines the level of noise in the capacitive scan.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetNoiseThreshold(uint32 widget)
{
    return (CSD_Touchpad_noiseThreshold[widget]);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetFingerHysteresis
********************************************************************************
*
* Summary:
*  This function reads the Hysteresis value from the component.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  This API returns the Hysteresis of the widget indicated by the argument.
*
* Global Variables:
*  CSD_Touchpad_hysteresis[] - This array contains the hysteresis value
*   for each widget.
*  CSD_Touchpad_widgetNumberSld - structure with dynamic sliders' parameters.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetFingerHysteresis(uint32 widget)
{
    return(CSD_Touchpad_hysteresis[widget]);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_GetNegativeNoiseThreshold
********************************************************************************
*
* Summary:
*  This function reads the negative noise threshold from the component.
*
* Parameters:
*  None.
*
* Return Value:
*  This API returns the negative noise threshold
*
* Global Variables:
*  CSD_Touchpad_negativeNoiseThreshold - This variable specifies the negative
*   difference between the raw count and baseline levels for Baseline resetting
*   to the raw count level.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetNegativeNoiseThreshold(uint32 widget)
{
#if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    return(CSD_Touchpad_negativeNoiseThreshold[widget]);
#else
    return(CSD_Touchpad_NEGATIVE_NOISE_THRESHOLD);
#endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */
}


#if(CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetFingerThreshold
    ********************************************************************************
    *
    * Summary:
    *  This API sets the finger threshold value for a widget.
    *
    * Parameters:
    *  sensorNumber:  widget index.
    *  value:  Finger threshold value for the widget.
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_fingerThreshold[] - This array contains the level of signal
    *   for each widget that determines if a finger is present on the widget.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetFingerThreshold(uint32 widget, uint8 value)
    {
        CSD_Touchpad_fingerThreshold[widget] = value;
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetNoiseThreshold
    ********************************************************************************
    *
    * Summary:
    *  This API sets the Noise Threshold value for each widget.
    *
    * Parameters:
    *  widget:  Sensor index number.
    *  value:   Noise Threshold value for widget.
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_noiseThreshold[] - This array contains the level of signal
    *   for each widget that determines the level of noise in the capacitive scan.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetNoiseThreshold(uint32 widget, uint8 value)
    {
        CSD_Touchpad_noiseThreshold[widget] = value;
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetFingerHysteresis
    ********************************************************************************
    *
    * Summary:
    *  This API sets the Hysteresis value of a widget
    *
    * Parameters:
    *  value:  Hysteresis value for widgets.
    *  widget:  widget number
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_hysteresis[] - This array contains the hysteresis value for each widget.
    *  CSD_Touchpad_widgetNumberSld - structure with dynamic sliders' parameters.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetFingerHysteresis(uint32 widget, uint8 value)
    {
        CSD_Touchpad_hysteresis[widget] = value;
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetNegativeNoiseThreshold
    ********************************************************************************
    *
    * Summary:
    *  This API sets the Negative Noise Threshold value of a widget
    *
    * Parameters:
    *  value:  Negative Noise Threshold value for widgets.
    *  widget: widget number
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_negativeNoiseThreshold  - This parameter specifies the negative
    *   difference between the raw count and baseline levels for Baseline resetting to
    *   the raw count level.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetNegativeNoiseThreshold(uint32 widget, uint8 value)
    {
        CSD_Touchpad_negativeNoiseThreshold[widget] = value;
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetDebounce
    ********************************************************************************
    *
    * Summary:
    *  This API sets the debounce value for a widget.
    *
    * Parameters:
    *  value:  Debounce value for widget.
    *  widget: widget index.
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_debounce[] - This array contains the debounce value for each widget.
    *  CSD_Touchpad_widgetNumberSld - structure with dynamic sliders' parameters.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetDebounce(uint32 widget, uint8 value)
    {
        CSD_Touchpad_debounce[widget] = value;
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_SetLowBaselineReset
    ********************************************************************************
    *
    * Summary:
    *  This API sets the low baseline reset threshold value for the sensor.
    *
    * Parameters:
    *  value: low baseline reset threshold value.
    *  sensor: Sensor index.
    *
    * Return Value:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_lowBaselineReset[] - This array contains the Baseline update
    *  threshold value for each sensor.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_SetLowBaselineReset(uint32 sensor, uint8 value)
    {
        CSD_Touchpad_lowBaselineReset[sensor] = value;
    }
#endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */


/*******************************************************************************
* Function Name: CSD_Touchpad_CheckIsSensorActive
********************************************************************************
*
* Summary:
*  Compares the CSD_Touchpad_sensorSignal[sensor] array element to the finger
*  threshold of the widget it belongs to. The hysteresis and debounce are taken into
*  account. The hysteresis is added or subtracted from the finger threshold
*  based on whether the sensor is currently active.
*  If the sensor is active, the threshold is lowered by the hysteresis amount.
*  If the sensor is inactive, the threshold is raised by the hysteresis amount.
*  The debounce counter is added to the sensor active transition.
*  This function updates the CSD_Touchpad_sensorOnMask[] array element.
*
* Parameters:
*  sensor:  Sensor number.
*
* Return:
*  Returns sensor state 1 if active, 0 if not active.
*
* Global Variables:
*  CSD_Touchpad_sensorSignal[] - used to store the difference between
*  the current value of raw data and a previous value of the baseline.
*  CSD_Touchpad_debounceCounter[]   - used to store the current debounce
*  counter of the sensor. the widget which have this parameter are buttons, matrix
*  buttons, proximity, and guard. All other widgets don't have the  debounce parameter
*  and use the last element of this array with value 0 (it means no debounce).
*  CSD_Touchpad_sensorOnMask[] - used to store the sensors on/off state.
*  CSD_Touchpad_sensorOnMask[0] contains the masked bits for sensors
*   0 through 7 (sensor 0 is bit 0, sensor 1 is bit 1).
*  CSD_Touchpad_sensorEnableMask[1] contains the masked bits for
*  sensors 8 through 15 (if needed), and so on.
*  0 - sensor is inactive.
*  1 - sensor is active.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint32 CSD_Touchpad_CheckIsSensorActive(uint32 sensor)
{
    uint8 widget;
    uint8 debounce;
    uint8 debounceIndex;

    uint8 fingerThreshold;
    uint8 hysteresis;
    static uint8 CSD_Touchpad_debounceCounter[] = {
    0u, 
};



    /* Prepare to find debounce counter index */
    widget = CSD_Touchpad_widgetNumber[sensor];

    fingerThreshold = CSD_Touchpad_fingerThreshold[widget];
    hysteresis = CSD_Touchpad_hysteresis[widget];
    debounce = CSD_Touchpad_debounce[widget];

        debounceIndex = CSD_Touchpad_UNUSED_DEBOUNCE_COUNTER_INDEX;
    CSD_Touchpad_debounceCounter[debounceIndex] = 1u;


    /* Was on */
    if (0u != CSD_Touchpad_GetBitValue(CSD_Touchpad_sensorOnMask, sensor))
    {
        /* Hysteresis minus */
        if (CSD_Touchpad_sensorSignal[sensor] < (fingerThreshold - hysteresis))
        {
            CSD_Touchpad_SetBitValue(CSD_Touchpad_sensorOnMask, sensor, 0u);
            /* Sensor inactive - reset Debounce counter */
            CSD_Touchpad_debounceCounter[debounceIndex] = debounce;
        }
    }
    else    /* Was off */
    {
        /* Hysteresis plus */
        if (CSD_Touchpad_sensorSignal[sensor] >= (fingerThreshold + hysteresis))
        {
            CSD_Touchpad_debounceCounter[debounceIndex]--;
            /* Sensor active, decrement debounce counter */
            if(CSD_Touchpad_debounceCounter[debounceIndex] == 0u)
            {
                CSD_Touchpad_SetBitValue(CSD_Touchpad_sensorOnMask, sensor, 1u);
            }
        }
        else
        {
            /* Sensor inactive - reset Debounce counter */
            CSD_Touchpad_debounceCounter[debounceIndex] = debounce;
        }
    }

    return ((uint32)((0u != CSD_Touchpad_GetBitValue(CSD_Touchpad_sensorOnMask, sensor)) ? 1u : 0u));
}


/*******************************************************************************
* Function Name: CSD_Touchpad_CheckIsWidgetActive
********************************************************************************
*
* Summary:
*  Compares the CapSense_sensorSignal[] array element to the finger threshold of
* the widget it belongs to. The hysteresis and debounce are taken into account.
* The hysteresis is added or subtracted from the finger threshold based on whether
*  the sensor is currently active. If the sensor is active, the threshold is
*  lowered by the hysteresis amount. If the sensor is inactive, the threshold
*  is raised by the hysteresis amount.
* The debounce counter added to the sensor active transition.
* This function updates CapSense_sensorOnMask[] array element
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  Returns widget sensor state 1 if one or more sensors within widget is/are
*  active, 0 if all sensors within widget are inactive.
*
* Global Variables:
*  rawDataIndex[]: Contains the  1st sensor position in the widget
*
* Side Effects:
*  None
*
*******************************************************************************/
uint32 CSD_Touchpad_CheckIsWidgetActive(uint32 widget)
{
    uint32 rawIndex = (uint32)CSD_Touchpad_rawDataIndex[widget];
    uint32 numOfSensors = (uint32)CSD_Touchpad_numberOfSensors[widget] + rawIndex;
    uint32 state = 0u;

    /* Check all sensors of widget */
    do
    {
        if(CSD_Touchpad_CheckIsSensorActive((uint32)rawIndex) != 0u)
        {
            state = CSD_Touchpad_SENSOR_IS_ACTIVE;
        }
        rawIndex++;
    }
    while(rawIndex < numOfSensors);

    widget++;
    rawIndex = CSD_Touchpad_rawDataIndex[widget];
    numOfSensors = CSD_Touchpad_numberOfSensors[widget] + rawIndex;
    
    /* Check all sensors of the widget */
    do 
    {
        if (CSD_Touchpad_CheckIsSensorActive(rawIndex) != 0u)
        {
            state |= CSD_Touchpad_SENSOR_2_IS_ACTIVE;
        }
        rawIndex++;
    }
    while(rawIndex < numOfSensors);
    
    /* Define active as Row and Col cross */
    if ( ((state & CSD_Touchpad_SENSOR_1_IS_ACTIVE) != 0u) && \
         ((state & CSD_Touchpad_SENSOR_2_IS_ACTIVE) != 0u) ) 
    {
        state = CSD_Touchpad_WIDGET_IS_ACTIVE;
    }
    else
    {
        state = 0u;
    }


    return state;
}


/*******************************************************************************
* Function Name: CSD_Touchpad_CheckIsAnyWidgetActive
********************************************************************************
*
* Summary:
*  Compares all the sensors of the CSD_Touchpad_Signal[] array to their finger
*  threshold. Calls CSD_Touchpad_CheckIsWidgetActive() for each widget so
*  the CSD_Touchpad_sensorOnMask[] array is up to date after calling this
*  function.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  Returns 1 if any widget is active, 0 none of widgets are active.
*
* Global Variables:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
uint32 CSD_Touchpad_CheckIsAnyWidgetActive(void)
{

        uint32 i;
    uint32 state = 0u;
    uint32 realIndex = 0u;
    
    for(i = 0u; i < CSD_Touchpad_TOTAL_WIDGET_COUNT; i++)
    {
        if (CSD_Touchpad_CheckIsWidgetActive(realIndex) != 0u)
        {
            state = CSD_Touchpad_WIDGET_IS_ACTIVE;
        }
            realIndex += 2u;
    }
    


    return state;
}


/*******************************************************************************
* Function Name: CSD_Touchpad_EnableWidget
********************************************************************************
*
* Summary:
*  Enables all the widget elements (sensors) to the scanning process.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorEnableMask[] - used to store the sensor scanning
*  state.
*  CSD_Touchpad_sensorEnableMask[0] contains the masked bits for sensors
*  0 through 7 (sensor 0 is bit 0, sensor 1 is bit 1).
*  CSD_Touchpad_sensorEnableMask[1] contains the masked bits for
*  sensors 8 through 15 (if needed), and so on.
*  0 - sensor doesn't scan by CSD_Touchpad_ScanEnabledWidgets().
*  1 - sensor scans by CSD_Touchpad_ScanEnabledWidgets().
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_EnableWidget(uint32 widget)
{

    uint8 rawIndex = CSD_Touchpad_rawDataIndex[widget];
    uint8 numOfSensors = CSD_Touchpad_numberOfSensors[widget] + rawIndex;

    /* Enable all sensors of widget */
    do
    {
        CSD_Touchpad_SetBitValue(CSD_Touchpad_sensorEnableMask, (uint32)rawIndex, 1u);
        rawIndex++;
    }
    while(rawIndex < numOfSensors);
}


/*******************************************************************************
* Function Name: CSD_Touchpad_DisableWidget
********************************************************************************
*
* Summary:
*  Disables all the widget elements (sensors) from the scanning process.
*
* Parameters:
*  widget:  widget number.
*
* Return:
*  None
*
* Global Variables:
*  CSD_Touchpad_sensorEnableMask[] - used to store the sensor scanning
*  state.
*  CSD_Touchpad_sensorEnableMask[0] contains the masked bits for sensors
*  0 through 7 (sensor 0 is bit 0, sensor 1 is bit 1).
*  CSD_Touchpad_sensorEnableMask[1] contains the masked bits for
*  sensors 8 through 15 (if needed), and so on.
*  0 - sensor isn't scanned by CSD_Touchpad_ScanEnabledWidgets().
*  1 - sensor is scanned by CSD_Touchpad_ScanEnabledWidgets().
*
* Side Effects:
*  None
*
*******************************************************************************/
void CSD_Touchpad_DisableWidget(uint32 widget)
{
    uint8 rawIndex = CSD_Touchpad_rawDataIndex[widget];
    uint8 numOfSensors = CSD_Touchpad_numberOfSensors[widget] + rawIndex;

    /* Disable all sensors of widget */
    do
    {

        CSD_Touchpad_SetBitValue(CSD_Touchpad_sensorEnableMask, (uint32)rawIndex, 0u);
        rawIndex++;
    }
    while(rawIndex < numOfSensors);
}


#if(CSD_Touchpad_TOTAL_CENTROIDS_BASIC_COUNT)
    /*******************************************************************************
    * Function Name: CSD_Touchpad_FindMaximum
    ********************************************************************************
    *
    * Summary:
    *  Finds the index of the maximum element within a defined centroid. Checks
    *  CSD_Touchpad_sensorSignal[] within a defined centroid and
    *  returns the index of the maximum element. The values below the finger threshold are
    *  ignored.
    *  The centroid is defined by offset of the first element and a number of elements - count.
    *  The diplexed centroid requires at least two consecutive elements above
    *  FingerThreshold to find the index of the maximum element.
    *
    * Parameters:
    *  offset:  Start index of centroid in CSD_Touchpad_sensorSignal[] array.
    *  count:   number of elements within centroid.
    *  fingerThreshold:  Finger threshold.
    *  diplex:   pointer to diplex table.
    *
    * Return:
    *  Returns the index of the maximum element within a defined centroid.
    *  If the index of the maximum element isn't found it returns 0xFF.
    *
    * Global Variables:
    *  CSD_Touchpad_startOfSlider[] - contains the index of the first slider element
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
        uint8 CSD_Touchpad_FindMaximum(uint8 offset, uint8 count, uint8 fingerThreshold, const uint8 *diplex)
    #else
        uint8 CSD_Touchpad_FindMaximum(uint8 offset, uint8 count, uint8 fingerThreshold)
    #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */
    {
        uint8 i;
        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            uint8 curPos = 0u;
            /* No centroid at Start */
            uint8 curCntrdSize = 0u;
            uint8 curCtrdStartPos = CSD_Touchpad_MAXIMUM_CENTROID;
            /* Biggest centroid is zero */
            uint8 biggestCtrdSize = 0u;
            uint8 biggestCtrdStartPos = 0u;
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */
        uint8 maximum = CSD_Touchpad_MAXIMUM_CENTROID;

        uint8 temp = 0u;
        uint8 *startOfSlider = &CSD_Touchpad_sensorSignal[offset];

        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            if(diplex != 0u)
            {
                /* Initialize */
                i = 0u;

                /* Make slider x2 as Diplexed */
                count <<= 1u;
                while(1u)
                {
                    if (startOfSlider[curPos] > 0u)    /* Looking for centroids */
                    {
                        if (curCtrdStartPos == CSD_Touchpad_MAXIMUM_CENTROID)
                        {
                            /* Start of centroid */
                            curCtrdStartPos = i;
                            curCntrdSize++;
                        }
                        else
                        {
                            curCntrdSize++;
                        }
                    }
                    else   /* Select biggest and indicate zero start */
                    {
                        if(curCntrdSize > 0u)
                        {
                            /* We are at the end of current */
                            if(curCntrdSize > biggestCtrdSize)
                            {
                                biggestCtrdSize = curCntrdSize;
                                biggestCtrdStartPos = curCtrdStartPos;
                            }

                            curCntrdSize = 0u;
                            curCtrdStartPos = CSD_Touchpad_MAXIMUM_CENTROID;
                        }
                    }

                    i++;
                    curPos = diplex[i];
                    if(i == count)
                    {
                        break;
                    }
                }

                    /* Find biggest centroid if two are same size, last one wins
                       We are at the end of current */
                if (curCntrdSize >= biggestCtrdSize)
                {
                    biggestCtrdSize = curCntrdSize;
                    biggestCtrdStartPos = curCtrdStartPos;
                }
            }
            else
            {
                /* Without diplexing */
                biggestCtrdSize = count;
            }


            /* Check centroid size */
            #if (CSD_Touchpad_IS_NON_DIPLEX_SLIDER)
                if((biggestCtrdSize >= 2u) || ((biggestCtrdSize == 1u) && (diplex == 0u)))
            #else
                if(biggestCtrdSize >= 2u)
            #endif /* (CSD_Touchpad_IS_NON_DIPLEX_SLIDER) */
                {
                    for (i = biggestCtrdStartPos; i < (biggestCtrdStartPos + biggestCtrdSize); i++)
                    {
                        #if (CSD_Touchpad_IS_DIPLEX_SLIDER && CSD_Touchpad_IS_NON_DIPLEX_SLIDER)
                            if (diplex == 0u)
                            {
                                curPos = i;
                            }
                            else
                            {
                                curPos = diplex[i];
                            }
                        #elif (CSD_Touchpad_IS_DIPLEX_SLIDER)
                            curPos = diplex[i];
                        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER && CSD_Touchpad_IS_NON_DIPLEX_SLIDER) */
                        /* Looking for greater element within centroid */
                        if(startOfSlider[curPos] > fingerThreshold)
                        {
                            if(startOfSlider[curPos] > temp)
                            {
                                maximum = i;
                                temp = startOfSlider[curPos];
                            }
                        }
                    }
                }
        #else
            for (i = 0u; i < count; i++)
            {
                /* Looking for greater element within centroid */
                if(startOfSlider[i] > fingerThreshold)
                {
                    if(startOfSlider[i] > temp)
                    {
                        maximum = i;
                        temp = startOfSlider[i];
                    }
                }
            }
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */
        return (maximum);
    }


    /*******************************************************************************
    * Function Name: CSD_Touchpad_CalcCentroid
    ********************************************************************************
    *
    * Summary:
    *  Returns a position value calculated according to the index of the maximum element and API
    *  resolution.
    *
    * Parameters:
    *  type:  widget type.
    *  diplex:  pointer to diplex table.
    *  maximum:  Index of maximum element within centroid.
    *  offset:   Start index of centroid in CSD_Touchpad_sensorSignal[] array.
    *  count:    Number of elements within centroid.
    *  resolution:  multiplicator calculated according to centroid type and
    *  API resolution.
    *  noiseThreshold:  Noise threshold.
    *
    * Return:
    *  Returns a position value of the slider.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
#if (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT > 0u)
    uint8 CSD_Touchpad_CalcCentroid(uint8 maximum, uint8 offset,
                                        uint8 count, uint32 resolution, uint8 noiseThreshold)
#else
    uint8 CSD_Touchpad_CalcCentroid(uint8 maximum, uint8 offset,
                                        uint8 count, uint16 resolution, uint8 noiseThreshold)
#endif /* (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT > 0u) */
    {
        #if ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT > 0u))
            uint8 posPrev;
            uint8 posNext;
        #endif /* ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT>0u) || (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT>0u)) */

        /* Helps during centroid calculation */
        #if (CSD_Touchpad_TOTAL_CENTROIDS_BASIC_COUNT)
            static uint8 CSD_Touchpad_centroid[3u];
        #endif  /* (CSD_Touchpad_TOTAL_CENTROIDS_BASIC_COUNT) */

        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            uint8 pos;
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        uint8 position;
        uint32 numerator;
        int32 denominator;

        uint8  *startOfSlider = &CSD_Touchpad_sensorSignal[offset];

        #if ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT > 0u))
            posPrev = 0u;
        #endif /* ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT>0u) || (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT>0u)) */

        #if (CSD_Touchpad_ADD_SLIDER_TYPE)
            if(type == CSD_Touchpad_TYPE_RADIAL_SLIDER)
            {
        #endif /* (CSD_Touchpad_ADD_SLIDER_TYPE) */

            #if (CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u)
                /* Copy Signal to the centroid array */
                CSD_Touchpad_centroid[CSD_Touchpad_POS] = startOfSlider[maximum];

                /* Check borders for ROTARY Slider */
                if (maximum == 0u)                   /* Start of centroid */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = startOfSlider[count - 1u];
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = startOfSlider[1u];
                }
                else if (maximum == (count - 1u))    /* End of centroid */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = startOfSlider[maximum - 1u];
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = startOfSlider[0u];
                }
                else                                /* Not first Not last */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = startOfSlider[maximum - 1u];
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = startOfSlider[maximum + 1u];
                }
            #endif /* (CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) */

        #if (CSD_Touchpad_ADD_SLIDER_TYPE)
            }
            else
            {
        #endif /* (CSD_Touchpad_ADD_SLIDER_TYPE) */

            #if ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT > 0u))
                #if (CSD_Touchpad_IS_DIPLEX_SLIDER && CSD_Touchpad_IS_NON_DIPLEX_SLIDER)
                    /* Calculate next and previous near to maximum */
                    if(diplex == 0u)
                    {
                        pos     = maximum;
                        posPrev = maximum - 1u;
                        posNext = maximum + 1u;
                    }
                    else
                    {
                        pos     = diplex[maximum];
                        posPrev = diplex[maximum - 1u];
                        posNext = diplex[maximum + 1u];
                        count <<= 1u;
                    }
                #elif (CSD_Touchpad_IS_DIPLEX_SLIDER)
                    /* Calculate next and previous near to maximum */
                    pos     = diplex[maximum];
                    posPrev = diplex[maximum - 1u];
                    posNext = diplex[maximum + 1u];
                    count <<= 1u;
                #else
                    /* Calculate next and previous near to maximum */
                    if (maximum >= 1u)
                    {
                        posPrev = maximum - 1u;
                    }
                    posNext = maximum + 1u;
                #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER && CSD_Touchpad_IS_NON_DIPLEX_SLIDER) */

                /* Copy Signal to the centroid array */
                #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
                    CSD_Touchpad_centroid[CSD_Touchpad_POS] = startOfSlider[pos];
                #else
                    CSD_Touchpad_centroid[CSD_Touchpad_POS] = startOfSlider[maximum];
                #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

                /* Check borders for LINEAR Slider */
                if (maximum == 0u)                   /* Start of centroid */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = 0u;
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = startOfSlider[posNext];
                }
                else if (maximum == ((count) - 1u)) /* End of centroid */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = startOfSlider[posPrev];
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = 0u;
                }
                else                                /* Not first Not last */
                {
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = startOfSlider[posPrev];
                    CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = startOfSlider[posNext];
                }
            #endif /* ((CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT>0u)||(CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT>0u))*/

        #if (CSD_Touchpad_ADD_SLIDER_TYPE)
            }
        #endif /* (CSD_Touchpad_ADD_SLIDER_TYPE) */

        /* Subtract noiseThreshold */
        if(CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] > noiseThreshold)
        {
            CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] -= noiseThreshold;
        }
        else
        {
            CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] = 0u;
        }

        /* Maximum always greater than fingerThreshold, so greater than noiseThreshold */
        CSD_Touchpad_centroid[CSD_Touchpad_POS] -= noiseThreshold;

        /* Subtract noiseThreshold */
        if(CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] > noiseThreshold)
        {
            CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] -= noiseThreshold;
        }
        else
        {
            CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] = 0u;
        }


        /* Si+1 - Si-1 */
        numerator = (uint32) CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT] -
                    (uint32) CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV];

        /* Si+1 + Si + Si-1 */
        denominator = (int32) CSD_Touchpad_centroid[CSD_Touchpad_POS_PREV] +
                      (int32) CSD_Touchpad_centroid[CSD_Touchpad_POS] +
                      (int32) CSD_Touchpad_centroid[CSD_Touchpad_POS_NEXT];

        /* (numerator/denominator) + maximum */
        denominator = (((int32)(uint32)((uint32)numerator << 8u)/denominator) + (int32)(uint32)((uint32) maximum << 8u));

        #if(CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u)
            /* Only required for RADIAL Slider */
            if(denominator < 0)
            {
                denominator += (int32)((uint16)((uint16) count << 8u));
            }
        #endif /* (CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) */

        denominator *= (int16)resolution;

        /* Round result and put it to uint8 */
        position = ((uint8) HI16((uint32)denominator + CSD_Touchpad_CENTROID_ROUND_VALUE));

        return (position);
    }
#endif /* (CSD_Touchpad_TOTAL_CENTROIDS_BASIC_COUNT) */


#if((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u))
    /*******************************************************************************
    * Function Name: CSD_Touchpad_GetCentroidPos
    ********************************************************************************
    *
    * Summary:
    *  Checks the CSD_Touchpad_Signal[ ] array for a centroid within
    *  a slider specified range. The centroid position is calculated according to the resolution
    *  specified in the CapSense customizer. The position filters are applied to the
    *  result if enabled.
    *
    * Parameters:
    *  widget:  Widget number.
    *  For every linear slider widget there are defines in this format:
    *  #define CSD_Touchpad_"widget_name"__LS           5
    *
    * Return:
    *  Returns a position value of the linear slider.
    *
    * Global Variables:
    *  None
    *
    * Side Effects:
    *  If any sensor within the slider widget is active, the function returns values
    *  from zero to the API resolution value set in the CapSense customizer. If no
    *  sensors are active, the function returns 0xFFFF. If an error occurs during
    *  execution of the centroid/diplexing algorithm, the function returns 0xFFFF.
    *  There are no checks of the widget type argument provided to this function.
    *  The unproper widget type provided will cause unexpected position calculations.
    *
    * Note:
    *  If noise counts on the slider segments are greater than the noise
    *  threshold, this subroutine may generate a false centroid result. The noise
    *  threshold should be set carefully (high enough above the noise level) so
    *  that noise will not generate a false centroid.
    *******************************************************************************/
    uint16 CSD_Touchpad_GetCentroidPos(uint32 widget)
    {
        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            const uint8 *diplex;
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        #if (0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)
            uint8 posIndex;
            uint8 firstTimeIndex = CSD_Touchpad_posFiltersData[widget];
            uint8 posFiltersMask = CSD_Touchpad_posFiltersMask[widget];
        #endif /* (0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK) */

        #if ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)) || \
             (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)))
            uint8 tempPos;
        #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)) || \
               *   (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)))
               */

        uint8 maximum;
        uint16 position;
        uint8 offset = CSD_Touchpad_rawDataIndex[widget];
        uint8 count = CSD_Touchpad_numberOfSensors[widget];

        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            if(widget < CSD_Touchpad_TOTAL_DIPLEXED_SLIDERS_COUNT)
            {
                maximum = CSD_Touchpad_diplexTable[widget];
                diplex = &CSD_Touchpad_diplexTable[maximum];
            }
            else
            {
                diplex = 0u;
            }
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        /* Find Maximum within centroid */
        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            maximum = CSD_Touchpad_FindMaximum(offset, count, (uint8)CSD_Touchpad_fingerThreshold[widget], diplex);
        #else
            maximum = CSD_Touchpad_FindMaximum(offset, count, (uint8)CSD_Touchpad_fingerThreshold[widget]);
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        if (maximum != 0xFFu)
        {
            /* Calculate centroid */
            position = (uint16) CSD_Touchpad_CalcCentroid(maximum,
                         offset, count, CSD_Touchpad_centroidMult[widget], CSD_Touchpad_noiseThreshold[widget]);

            #if (0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)
                /* Check if this linear slider has enabled filters */
                if (0u != (posFiltersMask & CSD_Touchpad_ANY_POS_FILTER))
                {
                    /* Calculate position to store filters data */
                    posIndex  = firstTimeIndex + 1u;

                    if (0u == CSD_Touchpad_posFiltersData[firstTimeIndex])
                    {
                        /* Init filters */
                        CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                        #if ((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                                     CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)) || \
                             (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                     CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)))

                            if ( (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER)) ||
                                 (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER)) )
                            {
                                CSD_Touchpad_posFiltersData[posIndex + 1u] = (uint8) position;
                            }
                        #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                               *           CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)) || \
                               *   (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                               *           CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)))
                               */

                        CSD_Touchpad_posFiltersData[firstTimeIndex] = 1u;
                    }
                    else
                    {
                        /* Do filtering */
                        #if (0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER))
                            {
                                tempPos = (uint8) position;
                                position = CSD_Touchpad_MedianFilter(position,
                                                                (uint16)CSD_Touchpad_posFiltersData[posIndex],
                                                                (uint16)CSD_Touchpad_posFiltersData[posIndex + 1u]);
                                CSD_Touchpad_posFiltersData[posIndex + 1u] =
                                                                             CSD_Touchpad_posFiltersData[posIndex];
                                CSD_Touchpad_posFiltersData[posIndex] = tempPos;
                            }
                        #endif /*(0u != (CSD_Touchpad_MEDIAN_FILTER &
                               *         CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if(0u!=(CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER))
                            {
                                tempPos = (uint8) position;
                                position = CSD_Touchpad_AveragingFilter(position,
                                                                (uint16)CSD_Touchpad_posFiltersData[posIndex],
                                                                (uint16)CSD_Touchpad_posFiltersData[posIndex + 1u]);
                                CSD_Touchpad_posFiltersData[posIndex+1u]=CSD_Touchpad_posFiltersData[posIndex];
                                CSD_Touchpad_posFiltersData[posIndex] = tempPos;
                            }
                        #endif /* (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                               *           CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_IIR2_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_IIR2_FILTER))
                            {
                                position = CSD_Touchpad_IIR2Filter(position,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_IIR2_FILTER & \
                               *          CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_IIR4_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_IIR4_FILTER))
                            {
                                position = CSD_Touchpad_IIR4Filter(position,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_IIR4_FILTER & \
                               *          CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_JITTER_FILTER & CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_JITTER_FILTER))
                            {
                                position = CSD_Touchpad_JitterFilter(position,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_JITTER_FILTER & \
                               *           CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK))
                               */
                    }
                }
            #endif /* (0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK) */

        }
        else
        {
            /* Maximum wasn't found */
            position = 0xFFFFu;

            #if(0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK)
                /* Reset filters */
                if(0u != (posFiltersMask & CSD_Touchpad_ANY_POS_FILTER))
                {
                    CSD_Touchpad_posFiltersData[firstTimeIndex] = 0u;
                }
            #endif /* (0u != CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK) */
        }


        return (position);
    }
#endif /* ((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u)) */


#if((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u))
    /*******************************************************************************
    * Function Name: CSD_Touchpad_GetRadialCentroidPos
    ********************************************************************************
    *
    * Summary:
    *  Checks the CSD_Touchpad_Signal[ ] array for a centroid within
    *  a slider specified range. The centroid position is calculated according to the resolution
    *  specified in the CapSense customizer. The position filters are applied to the
    *  result if enabled.
    *
    * Parameters:
    *  widget:  Widget number.
    *  For every radial slider widget there are defines in this format:
    *  #define CSD_Touchpad_"widget_name"__RS           5
    *
    * Return:
    *  Returns a position value of the radial slider.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  If any sensor within the slider widget is active, the function returns values
    *  from zero to the API resolution value set in the CapSense customizer. If no
    *  sensors are active, the function returns 0xFFFF.
    *  There are no checks of the widget type argument provided to this function.
    *  The unproper widget type provided will cause unexpected position calculations.
    *
    * Note:
    *  If noise counts on the slider segments are greater than the noise
    *  threshold, this subroutine may generate a false centroid result. The noise
    *  threshold should be set carefully (high enough above the noise level) so
    *  that noise will not generate a false centroid.
    *
    *******************************************************************************/
     uint16 CSD_Touchpad_GetRadialCentroidPos(uint32 widget)
    {
        #if (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)
            uint8 posIndex;
            uint8 firstTimeIndex = CSD_Touchpad_posFiltersData[widget];
            uint8 posFiltersMask = CSD_Touchpad_posFiltersMask[widget];
        #endif /* (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK) */

        #if ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)) || \
             (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)))
            uint8 tempPos;
        #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)) || \
               *   (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)))
               */

        uint8 maximum;
        uint16 position;
        uint8 offset = CSD_Touchpad_rawDataIndex[widget];
        uint8 count = CSD_Touchpad_numberOfSensors[widget];

        /* Find Maximum within centroid */
        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            maximum = CSD_Touchpad_FindMaximum(offset, count, (uint8)CSD_Touchpad_fingerThreshold[widget], 0u);
        #else
            maximum = CSD_Touchpad_FindMaximum(offset, count, (uint8)CSD_Touchpad_fingerThreshold[widget]);
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        if (maximum != CSD_Touchpad_MAXIMUM_CENTROID)
        {
            /* Calculate centroid */
            position = (uint16) CSD_Touchpad_CalcCentroid(maximum,
                         offset, count, CSD_Touchpad_centroidMult[widget], CSD_Touchpad_noiseThreshold[widget]);

            #if (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)
                /* Check if this Radial slider has enabled filters */
                if (0u != (posFiltersMask & CSD_Touchpad_ANY_POS_FILTER))
                {
                    /* Calculate position to store filters data */
                    posIndex  = firstTimeIndex + 1u;

                    if (0u == CSD_Touchpad_posFiltersData[firstTimeIndex])
                    {
                        /* Init filters */
                        CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                        #if ((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                                     CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)) || \
                             (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                     CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)))

                            if ( (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER))  ||
                                 (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER)) )
                            {
                                CSD_Touchpad_posFiltersData[posIndex + 1u] = (uint8) position;
                            }
                        #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                               *           CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)) || \
                               *   (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                               *           CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)))
                               */

                        CSD_Touchpad_posFiltersData[firstTimeIndex] = 1u;
                    }
                    else
                    {
                        /* Do filtering */
                        #if (0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER))
                            {
                                tempPos = (uint8) position;
                                position = CSD_Touchpad_MedianFilter(position,
                                                                        CSD_Touchpad_posFiltersData[posIndex],
                                                                        CSD_Touchpad_posFiltersData[posIndex + 1u]);
                                CSD_Touchpad_posFiltersData[posIndex + 1u] =
                                                                              CSD_Touchpad_posFiltersData[posIndex];
                                CSD_Touchpad_posFiltersData[posIndex] = tempPos;
                            }
                        #endif /* (0u != (CSD_Touchpad_MEDIAN_FILTER &
                               *          CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                    CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER))
                            {
                                tempPos = (uint8) position;
                                position = CSD_Touchpad_AveragingFilter(position,
                                                                       CSD_Touchpad_posFiltersData[posIndex],
                                                                       CSD_Touchpad_posFiltersData[posIndex + 1u]);
                                CSD_Touchpad_posFiltersData[posIndex+1u]= CSD_Touchpad_posFiltersData[posIndex];
                                CSD_Touchpad_posFiltersData[posIndex] = tempPos;
                            }
                        #endif /* (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                               *          CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_IIR2_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_IIR2_FILTER))
                            {
                                position = CSD_Touchpad_IIR2Filter(position,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_IIR2_FILTER &
                               *          CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_IIR4_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_IIR4_FILTER))
                            {
                                position = CSD_Touchpad_IIR4Filter(position,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_IIR4_FILTER &
                               *          CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                               */

                        #if (0u != (CSD_Touchpad_JITTER_FILTER & CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                            if (0u != (posFiltersMask & CSD_Touchpad_JITTER_FILTER))
                            {
                                position = CSD_Touchpad_JitterFilter(position,
                                                                         CSD_Touchpad_posFiltersData[posIndex]);
                                CSD_Touchpad_posFiltersData[posIndex] = (uint8) position;
                            }
                        #endif /* (0u != (CSD_Touchpad_JITTER_FILTER &
                               *           CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK))
                               */
                    }
                }
            #endif /* (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK) */

        }
        else
        {
            /* Maximum was not found */
            position = 0xFFFFu;

            #if (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK)
                /* Reset filters */
                if((posFiltersMask & CSD_Touchpad_ANY_POS_FILTER) != 0u)
                {
                    CSD_Touchpad_posFiltersData[firstTimeIndex] = 0u;
                }
            #endif /* (0u != CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK) */
        }

        return (position);
    }
#endif /* ((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u)) */


#if(CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT > 0u)
    /*******************************************************************************
    * Function Name: CSD_Touchpad_GetTouchCentroidPos
    ********************************************************************************
    *
    * Summary:
    *  If a finger is present on a touchpad, this function calculates the X and Y
    *  position of the finger by calculating the centroids within touchpad specified
    *  range. The X and Y positions are calculated according to the API resolutions set in the
    *  CapSense customizer. Returns 1 if a finger is on the touchpad.
    *  The position filter is applied to the result if enabled.
    *  This function is available only if a touch pad is defined by the CapSense
    *  customizer.
    *
    * Parameters:
    *  widget:  Widget number.
    *  For every touchpad widget there are defines in this format:
    *  #define CSD_Touchpad_"widget_name"__TP            5
    *
    *  pos:     Pointer to the array of two uint16 elements, where result
    *  result of calculation of X and Y position are stored.
    *  pos[0u]  - position of X
    *  pos[1u]  - position of Y
    *
    * Return:
    *  Returns a 1 if a finger is on the touch pad, 0 - if not.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *   There are no checks of the widget type argument provided to this function.
    *   The unproper widget type provided will cause unexpected position
    *   calculations.
    *
    *******************************************************************************/
    uint32 CSD_Touchpad_GetTouchCentroidPos(uint32 widget, uint16* pos)
    {
        #if (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)
            uint8 posXIndex;
            uint8 posYIndex;
            uint8 firstTimeIndex = CSD_Touchpad_posFiltersData[widget];
            uint8 posFiltersMask = CSD_Touchpad_posFiltersMask[widget];
        #endif /* (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK) */

        #if ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)) || \
             (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)))
            uint16 tempPos;
        #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)) || \
               *   (0u != (CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)))
               */

        uint8 MaxX;
        uint8 MaxY;
        uint8 posX;
        uint8 posY;
        uint32 touch = 0u;
        uint8 offset = CSD_Touchpad_rawDataIndex[widget];
        uint8 count = CSD_Touchpad_numberOfSensors[widget];

        /* Find Maximum within X centroid */
        #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
            MaxX = CSD_Touchpad_FindMaximum(offset, count, CSD_Touchpad_fingerThreshold[widget], 0u);
        #else
            MaxX = CSD_Touchpad_FindMaximum(offset, count, CSD_Touchpad_fingerThreshold[widget]);
        #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

        if (MaxX != CSD_Touchpad_MAXIMUM_CENTROID)
        {
            offset = CSD_Touchpad_rawDataIndex[widget + 1u];
            count = CSD_Touchpad_numberOfSensors[widget + 1u];

            /* Find Maximum within Y centroid */
            #if (CSD_Touchpad_IS_DIPLEX_SLIDER)
                MaxY = CSD_Touchpad_FindMaximum(offset, count, CSD_Touchpad_fingerThreshold[widget + 1u], 0u);
            #else
                MaxY = CSD_Touchpad_FindMaximum(offset, count, CSD_Touchpad_fingerThreshold[widget + 1u]);
            #endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

            if (MaxY != CSD_Touchpad_MAXIMUM_CENTROID)
            {
                /* X and Y maximums are found = true touch */
                touch = 1u;

                /* Calculate Y centroid */
                posY = CSD_Touchpad_CalcCentroid(MaxY, offset, count,
                            CSD_Touchpad_centroidMult[widget + 1u], CSD_Touchpad_noiseThreshold[widget + 1u]);

                /* Calculate X centroid */
                offset = CSD_Touchpad_rawDataIndex[widget];
                count = CSD_Touchpad_numberOfSensors[widget];

                posX = CSD_Touchpad_CalcCentroid(MaxX, offset, count,
                            CSD_Touchpad_centroidMult[widget],CSD_Touchpad_noiseThreshold[widget]);

                #if (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)
                    /* Check if this TP has enabled filters */
                    if (0u != (posFiltersMask & CSD_Touchpad_ANY_POS_FILTER))
                    {
                        /* Calculate position to store filters data */
                        posXIndex  = firstTimeIndex + 1u;
                        posYIndex  = CSD_Touchpad_posFiltersData[widget + 1u];

                        if (0u == CSD_Touchpad_posFiltersData[firstTimeIndex])
                        {
                            /* Init filters */
                            CSD_Touchpad_posFiltersData[posXIndex] = posX;
                            CSD_Touchpad_posFiltersData[posYIndex] = posY;

                            #if((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                                        CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))|| \
                                (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                        CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)))

                                if ( (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER)) ||
                                     (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER)) )
                                {
                                    CSD_Touchpad_posFiltersData[posXIndex + 1u] = posX;
                                    CSD_Touchpad_posFiltersData[posYIndex + 1u] = posY;
                                }
                            #endif /* ((0u != (CSD_Touchpad_MEDIAN_FILTER & \
                                   *           CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)) || \
                                   *    (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                   *            CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)))
                                   */

                            CSD_Touchpad_posFiltersData[firstTimeIndex] = 1u;
                        }
                        else
                        {
                            /* Do filtering */
                            #if (0u != (CSD_Touchpad_MEDIAN_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                if (0u != (posFiltersMask & CSD_Touchpad_MEDIAN_FILTER))
                                {
                                    tempPos = posX;
                                    posX = (uint8) CSD_Touchpad_MedianFilter(posX,
                                                                      CSD_Touchpad_posFiltersData[posXIndex],
                                                                      CSD_Touchpad_posFiltersData[posXIndex + 1u]);
                                    CSD_Touchpad_posFiltersData[posXIndex + 1u] =
                                                                             CSD_Touchpad_posFiltersData[posXIndex];
                                    CSD_Touchpad_posFiltersData[posXIndex] = tempPos;

                                    tempPos = posY;
                                    posY = (uint8) CSD_Touchpad_MedianFilter(posY,
                                                                       CSD_Touchpad_posFiltersData[posYIndex],
                                                                       CSD_Touchpad_posFiltersData[posYIndex + 1u]);
                                    CSD_Touchpad_posFiltersData[posYIndex + 1u] =
                                                                             CSD_Touchpad_posFiltersData[posYIndex];
                                    CSD_Touchpad_posFiltersData[posYIndex] = tempPos;
                                }

                            #endif /* (0u != (CSD_Touchpad_MEDIAN_FILTER & \
                                   *          CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                   */

                            #if(0u !=(CSD_Touchpad_AVERAGING_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                if (0u != (posFiltersMask & CSD_Touchpad_AVERAGING_FILTER))
                                {
                                    tempPos = posX;
                                    posX = (uint8) CSD_Touchpad_AveragingFilter(posX,
                                                                       CSD_Touchpad_posFiltersData[posXIndex],
                                                                       CSD_Touchpad_posFiltersData[posXIndex + 1u]);
                                    CSD_Touchpad_posFiltersData[posXIndex + 1u] =
                                                                             CSD_Touchpad_posFiltersData[posXIndex];
                                    CSD_Touchpad_posFiltersData[posXIndex] = tempPos;

                                    tempPos = posY;
                                    posY = (uint8) CSD_Touchpad_AveragingFilter(posY,
                                                                      CSD_Touchpad_posFiltersData[posYIndex],
                                                                      CSD_Touchpad_posFiltersData[posYIndex + 1u]);
                                    CSD_Touchpad_posFiltersData[posYIndex + 1u] =
                                                                            CSD_Touchpad_posFiltersData[posYIndex];
                                    CSD_Touchpad_posFiltersData[posYIndex] = tempPos;
                                }

                            #endif /* (0u != (CSD_Touchpad_AVERAGING_FILTER & \
                                   *           CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                   */

                            #if (0u != (CSD_Touchpad_IIR2_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                if (0u != (posFiltersMask & CSD_Touchpad_IIR2_FILTER))
                                {
                                    posX = (uint8) CSD_Touchpad_IIR2Filter(posX,
                                                                           CSD_Touchpad_posFiltersData[posXIndex]);
                                    CSD_Touchpad_posFiltersData[posXIndex] = posX;

                                    posY = (uint8) CSD_Touchpad_IIR2Filter(posY,
                                                                            CSD_Touchpad_posFiltersData[posYIndex]);
                                    CSD_Touchpad_posFiltersData[posYIndex] = posY;
                                }

                            #endif /* (0u != (CSD_Touchpad_IIR2_FILTER & \
                                   *          CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                   */

                            #if (0u != (CSD_Touchpad_IIR4_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                if (0u != (posFiltersMask & CSD_Touchpad_IIR4_FILTER))
                                {
                                    posX = (uint8) CSD_Touchpad_IIR4Filter((uint16)posX,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posXIndex]);
                                    CSD_Touchpad_posFiltersData[posXIndex] = posX;

                                    posY = (uint8) CSD_Touchpad_IIR4Filter((uint16)posY,
                                                                    (uint16)CSD_Touchpad_posFiltersData[posYIndex]);
                                    CSD_Touchpad_posFiltersData[posYIndex] = posY;
                                }

                            #endif /* (0u != (CSD_Touchpad_IIR4_FILTER & \
                                   *           CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                   */

                            #if (0u != (CSD_Touchpad_JITTER_FILTER & CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                if (0u != (posFiltersMask & CSD_Touchpad_JITTER_FILTER))
                                    {
                                        posX = (uint8) CSD_Touchpad_JitterFilter(posX,
                                                                            CSD_Touchpad_posFiltersData[posXIndex]);
                                        CSD_Touchpad_posFiltersData[posXIndex] = posX;

                                        posY = (uint8) CSD_Touchpad_JitterFilter(posY,
                                                                            CSD_Touchpad_posFiltersData[posYIndex]);
                                        CSD_Touchpad_posFiltersData[posYIndex] = posY;
                                    }
                            #endif /* (0u != (CSD_Touchpad_JITTER_FILTER & \
                                   *           CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK))
                                   */
                        }
                    }
                #endif /* (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK) */

                /* Save positions */
                pos[0u] = posX;
                pos[1u] = posY;
            }
        }

        #if (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK)
            if(touch == 0u)
            {
                /* Reset filters */
                if ((posFiltersMask & CSD_Touchpad_ANY_POS_FILTER) != 0u)
                {
                    CSD_Touchpad_posFiltersData[firstTimeIndex] = 0u;
                }
            }
        #endif /* (0u != CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK) */

        return (touch);
    }
#endif /* (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT > 0u) */


#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      ((CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)) )
    /*******************************************************************************
    * Function Name: CSD_Touchpad_MedianFilter
    ********************************************************************************
    *
    * Summary:
    *  This is the Median filter function.
    *  The median filter looks at the three most recent samples and reports the
    *  median value.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *  x3:  Before previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_MedianFilter(uint16 x1, uint16 x2, uint16 x3)
    {
        uint16 tmp;

        if (x1 > x2)
        {
            tmp = x2;
            x2 = x1;
            x1 = tmp;
        }

        if (x2 > x3)
        {
            x2 = x3;
        }

        return ((x1 > x2) ? x1 : x2);
    }
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      ((CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)) ) */


#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_AVERAGING_FILTER)) )
    /*******************************************************************************
    * Function Name: CSD_Touchpad_AveragingFilter
    ********************************************************************************
    *
    * Summary:
    *  This is the Averaging filter function.
    *  The averaging filter looks at the three most recent samples of a position and
    *  reports the averaging value.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *  x3:  Before previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_AveragingFilter(uint16 x1, uint16 x2, uint16 x3)
    {
        uint32 tmp = ((uint32)x1 + (uint32)x2 + (uint32)x3) / 3u;

        return ((uint16) tmp);
    }
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_AVERAGING_FILTER) ) */


#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR2_FILTER)) )
    /*******************************************************************************
    * Function Name: CSD_Touchpad_IIR2Filter
    ********************************************************************************
    *
    * Summary:
    *  This is the IIR1/2 filter function. IIR1/2 = 1/2current + 1/2previous.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_IIR2Filter(uint16 x1, uint16 x2)
    {
        uint32 tmp;

        /* IIR = 1/2 Current Value+ 1/2 Previous Value */
        tmp = (uint32)x1 + (uint32)x2;
        tmp >>= 1u;

        return ((uint16) tmp);
    }
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER)) || \
       *    (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR2_FILTER)) )
       */


#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR4_FILTER)) )
    /*******************************************************************************
    * Function Name: CSD_Touchpad_IIR4Filter
    ********************************************************************************
    *
    * Summary:
    *  This is the IIR1/4 filter function. IIR1/4 = 1/4current + 3/4previous.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *
    * Return:
    *  Returns a filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_IIR4Filter(uint16 x1, uint16 x2)
    {
        uint32 tmp;

        /* IIR = 1/4 Current Value + 3/4 Previous Value */
        tmp = (uint32)x1 + (uint32)x2;
        tmp += ((uint32)x2 << 1u);
        tmp >>= 2u;

        return ((uint16) tmp);
    }
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER)) || \
       *    (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR4_FILTER)) )
       */


#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) || \
      (0u != (CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)))
    /*******************************************************************************
    * Function Name: uint16 CSD_Touchpad_JitterFilter
    ********************************************************************************
    *
    * Summary:
    *  This is the Jitter filter function.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_JitterFilter(uint16 x1, uint16 x2)
    {
        if (x1 > x2)
        {
            x1--;
        }
        else
        {
            if (x1 < x2)
            {
                x1++;
            }
        }

        return x1;
    }
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)) || \
       *    (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) )
       *    (0u != (CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) )
       */


#if (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER))
    /*******************************************************************************
    * Function Name: CSD_Touchpad_IIR8Filter
    ********************************************************************************
    *
    * Summary:
    *  This is the IIR1/8 filter function. IIR1/8 = 1/8current + 7/8previous.
    *  Only applies for raw data.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_IIR8Filter(uint16 x1, uint16 x2)
    {
        uint32 tmp;

        /* IIR = 1/8 Current Value + 7/8 Previous Value */
        tmp = (uint32)x1;
        tmp += (((uint32)x2 << 3u) - ((uint32)x2));
        tmp >>= 3u;

        return ((uint16) tmp);
    }
#endif /* (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER)) */


#if (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER))
    /*******************************************************************************
    * Function Name: CSD_Touchpad_IIR16Filter
    ********************************************************************************
    *
    * Summary:
    *  This is the IIR1/16 filter function. IIR1/16 = 1/16current + 15/16previous.
    *  Only applies for raw data.
    *
    * Parameters:
    *  x1:  Current value.
    *  x2:  Previous value.
    *
    * Return:
    *  Returns filtered value.
    *
    * Global Variables:
    *  None.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint16 CSD_Touchpad_IIR16Filter(uint16 x1, uint16 x2)
    {
        uint32 tmp;

        /* IIR = 1/16 Current Value + 15/16 Previous Value */
        tmp = (uint32)x1;
        tmp += (((uint32)x2 << 4u) - ((uint32)x2));
        tmp >>= 4u;

        return ((uint16) tmp);
    }
#endif /* (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER) */


#if (0u != (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT))

    /*******************************************************************************
    * Function Name: CSD_Touchpad_GetMatrixButtonPos
    ********************************************************************************
    *
    * Summary:
    *  Function calculates and returns a touch position (column and row) for the matrix
    *  button widget.
    *
    * Parameters:
    *  widget:  widget number;
    *  pos:     pointer to an array of two uint8, where touch position will be
    *           stored:
    *           pos[0] - column position;
    *           pos[1] - raw position.
    *
    * Return:
    *  Returns 1 if row and column sensors of matrix button are active, 0 - in other
    *  cases.
    *
    * Global Variables:
    *  CSD_Touchpad_fingerThreshold[ ] - used to store the finger threshold for all widgets.
    *  CSD_Touchpad_sensorSignal[ ] - used to store a difference between the current value of
    *  raw data and a previous value of the baseline.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    uint32 CSD_Touchpad_GetMatrixButtonPos(uint32 widget, uint8* pos)
    {
        uint8 i;
        uint32 retVal = 0u;
        uint16 row_sig_max = 0u;
        uint16 col_sig_max = 0u;
        uint8 row_ind = 0u;
        uint8 col_ind = 0u;

        if (CSD_Touchpad_CheckIsWidgetActive(widget) == 1u)
        {
            /* Find row number with maximal signal value */
            for(i = CSD_Touchpad_rawDataIndex[widget]; i < (CSD_Touchpad_rawDataIndex[widget] + \
                 CSD_Touchpad_numberOfSensors[widget]); i++)
            {
                if (CSD_Touchpad_sensorSignal[i] > col_sig_max)
                {
                    col_ind = i;
                    col_sig_max = CSD_Touchpad_sensorSignal[i];
                }
            }

            /* Find row number with maximal signal value */
            for(i = CSD_Touchpad_rawDataIndex[widget+1u]; i < (CSD_Touchpad_rawDataIndex[widget+1u] + \
                 CSD_Touchpad_numberOfSensors[widget+1u]); i++)
            {
                if (CSD_Touchpad_sensorSignal[i] > row_sig_max)
                {
                    row_ind = i;
                    row_sig_max = CSD_Touchpad_sensorSignal[i];
                }
            }

            if((col_sig_max >= CSD_Touchpad_fingerThreshold[widget]) && \
               (row_sig_max >= CSD_Touchpad_fingerThreshold[widget+1u]))
            {
                pos[0u] = col_ind - CSD_Touchpad_rawDataIndex[widget];
                pos[1u] = row_ind - CSD_Touchpad_rawDataIndex[widget+1u];
                retVal = 1u;
            }
        }
        return (retVal);
    }

#endif /* (0u != (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT)) */

/*******************************************************************************
* Function Name: CSD_Touchpad_GetWidgetNumber
********************************************************************************
*
* Summary:
*  This API returns the widget number for the sensor.
*
* Parameters:
*  sensor: sensor index. The value of index can be
*  from 0 to (CSD_Touchpad_TOTAL_SENSOR_COUNT-1).
*
* Return:
*  This API returns the widget number for the sensor.
*
* Global Variables:
*  CSD_Touchpad_widgetNumber[]  - stores widget numbers.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint32 CSD_Touchpad_GetWidgetNumber(uint32 sensor)
{
    return((uint32)CSD_Touchpad_widgetNumber[sensor]);
}

/*******************************************************************************
* Function Name: CSD_Touchpad_GetLowBaselineReset
********************************************************************************
*
* Summary:
*  This API returns the low baseline reset threshold value for the  sensor.
*
* Parameters:
*  sensor: sensor index. The value of index can be
*  from 0 to (CSD_Touchpad_TOTAL_SENSOR_COUNT-1).
*
* Return:
*  low baseline reset threshold value a sensor.
*
* Global Variables:
*  CSD_Touchpad_lowBaselineReset[]  - stores low baseline reset values.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetLowBaselineReset(uint32 sensor)
{
    return(CSD_Touchpad_lowBaselineReset[sensor]);
}

/*******************************************************************************
* Function Name: CSD_Touchpad_GetDebounce
********************************************************************************
*
* Summary:
*  This API returns a debounce value.
*
* Parameters:
*  sensor: sensor index. The value of index can be
*  from 0 to (CSD_Touchpad_TOTAL_SENSOR_COUNT-1).
*
* Return:
*  Debounce value
*
* Global Variables:
*  CSD_Touchpad_debounce[]  - stores the debounce value.
*
* Side Effects:
*  None
*
*******************************************************************************/
uint8 CSD_Touchpad_GetDebounce(uint32 widget)
{
    return(CSD_Touchpad_debounce[widget]);
}

#if (CSD_Touchpad_RAW_FILTER_MASK != 0u)
    /*******************************************************************************
    * Function Name: CSD_Touchpad_EnableRawDataFilters
    ********************************************************************************
    *
    * Summary:
    *  Enables the rawdata filters for the sensor signals.
    *
    * Parameters:
    *  None
    *
    * Return:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_enableRawFilters defines if the filters are enabled or not.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_EnableRawDataFilters(void)
    {
        CSD_Touchpad_enableRawFilters = CSD_Touchpad_RAW_FILTERS_ENABLED;
    }

    /*******************************************************************************
    * Function Name: CSD_Touchpad_DisableRawDataFilters
    ********************************************************************************
    *
    * Summary:
    *  Disables the rawdata filters for the sensor signals.
    *
    * Parameters:
    *  None
    *
    * Return:
    *  None
    *
    * Global Variables:
    *  CSD_Touchpad_enableRawFilters defines if the filters are enabled or not.
    *
    * Side Effects:
    *  None
    *
    *******************************************************************************/
    void CSD_Touchpad_DisableRawDataFilters(void)
    {
        CSD_Touchpad_enableRawFilters = CSD_Touchpad_RAW_FILTERS_DISABLED;
    }
#endif /* (CSD_Touchpad_RAW_FILTER_MASK != 0u) */

/* [] END OF FILE */
